package cn.gzjp.modules.logis.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;
import java.util.List;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import cn.gzjp.common.utils.DateUtils;
import cn.gzjp.common.utils.MyBeanUtils;
import cn.gzjp.common.persistence.Page;
import cn.gzjp.common.web.BaseController;
import cn.gzjp.common.utils.StringUtils;
import cn.gzjp.common.utils.excel.ExportExcel;
import cn.gzjp.common.utils.excel.ImportExcel;
import cn.gzjp.modules.logis.entity.LogisGoodsPic;
import cn.gzjp.modules.logis.service.LogisGoodsPicService;

/**
 * 货物图片Controller
 * @author zengcp
 * @version 2018-03-21
 */
@Controller
@RequestMapping(value = "${adminPath}/logis/logisGoodsPic")
public class LogisGoodsPicController extends BaseController {

	@Autowired
	private LogisGoodsPicService logisGoodsPicService;
	
	
	/**
	 * 货物图片列表页面
	 */
	@RequiresPermissions("logis:logisGoodsPic:list")
	@RequestMapping(value = {"list", ""})
	public String list(LogisGoodsPic logisGoodsPic, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LogisGoodsPic> page = logisGoodsPicService.findPage(new Page<LogisGoodsPic>(request, response), logisGoodsPic); 
		model.addAttribute("page", page);
		return "modules/logis/logisGoodsPicList";
	}

	/**
	 * 查看，增加，编辑货物图片表单页面
	 */
	@RequiresPermissions(value={"logis:logisGoodsPic:view","logis:logisGoodsPic:add","logis:logisGoodsPic:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(LogisGoodsPic logisGoodsPic, Model model) {
		model.addAttribute("logisGoodsPic", logisGoodsPic);
		return "modules/logis/logisGoodsPicForm";
	}

	/**
	 * 保存货物图片
	 */
	@RequiresPermissions(value={"logis:logisGoodsPic:add","logis:logisGoodsPic:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(LogisGoodsPic logisGoodsPic, Model model, RedirectAttributes redirectAttributes) throws Exception{
		if (!beanValidator(model, logisGoodsPic)){
			return form(logisGoodsPic, model);
		}
		if(!logisGoodsPic.getIsNewRecord()){//编辑表单保存
			LogisGoodsPic t = logisGoodsPicService.get(logisGoodsPic.getId());//从数据库取出记录的值
			MyBeanUtils.copyBeanNotNull2Bean(logisGoodsPic, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
			logisGoodsPicService.save(t);//保存
		}else{//新增表单保存
			logisGoodsPicService.save(logisGoodsPic);//保存
		}
		addMessage(redirectAttributes, "保存货物图片成功");
		return redirect("/logis/logisGoodsPic/?repage");
	}
	
	/**
	 * 删除货物图片
	 */
	@RequiresPermissions("logis:logisGoodsPic:del")
	@RequestMapping(value = "delete")
	public String delete(LogisGoodsPic logisGoodsPic, RedirectAttributes redirectAttributes) {
		logisGoodsPicService.delete(logisGoodsPic);
		addMessage(redirectAttributes, "删除货物图片成功");
		return redirect("/logis/logisGoodsPic/?repage");
	}
	
	/**
	 * 批量删除货物图片
	 */
	@RequiresPermissions("logis:logisGoodsPic:del")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(String ids, RedirectAttributes redirectAttributes) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			logisGoodsPicService.delete(logisGoodsPicService.get(id));
		}
		addMessage(redirectAttributes, "删除货物图片成功");
		return redirect("/logis/logisGoodsPic/?repage");
	}
	
	/**
	 * 导出excel文件
	 */
	@RequiresPermissions("logis:logisGoodsPic:export")
    @RequestMapping(value = "export", method=RequestMethod.POST)
    public String exportFile(LogisGoodsPic logisGoodsPic, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "货物图片"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
            Page<LogisGoodsPic> page = logisGoodsPicService.findPage(new Page<LogisGoodsPic>(request, response, -1), logisGoodsPic);
    		new ExportExcel("货物图片", LogisGoodsPic.class).setDataList(page.getList()).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出货物图片记录失败！失败信息："+e.getMessage());
		}
		return redirect("/logis/logisGoodsPic/?repage");
    }

	/**
	 * 导入Excel数据

	 */
	@RequiresPermissions("logis:logisGoodsPic:import")
    @RequestMapping(value = "import", method=RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
		try {
			int successNum = 0;
			int failureNum = 0;
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<LogisGoodsPic> list = ei.getDataList(LogisGoodsPic.class);
			for (LogisGoodsPic logisGoodsPic : list){
				try{
					logisGoodsPicService.save(logisGoodsPic);
					successNum++;
				}catch(ConstraintViolationException ex){
					failureNum++;
				}catch (Exception ex) {
					failureNum++;
				}
			}
			if (failureNum>0){
				failureMsg.insert(0, "，失败 "+failureNum+" 条货物图片记录。");
			}
			addMessage(redirectAttributes, "已成功导入 "+successNum+" 条货物图片记录"+failureMsg);
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入货物图片失败！失败信息："+e.getMessage());
		}
		return redirect("/logis/logisGoodsPic/?repage");
    }
	
	/**
	 * 下载导入货物图片数据模板
	 */
	@RequiresPermissions("logis:logisGoodsPic:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
		try {
            String fileName = "货物图片数据导入模板.xlsx";
    		List<LogisGoodsPic> list = Lists.newArrayList(); 
    		new ExportExcel("货物图片数据", LogisGoodsPic.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息："+e.getMessage());
		}
		return redirect("/logis/logisGoodsPic/?repage");
    }
	@ModelAttribute
	public LogisGoodsPic get(@RequestParam(required=false) String id) {
		LogisGoodsPic entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = logisGoodsPicService.get(id);
		}
		if (entity == null){
			entity = new LogisGoodsPic();
		}
		return entity;
	}
	

}